package com.steptime.designpattern.creational.single;

import java.util.concurrent.atomic.AtomicLong;


public class IdGenertaor {
    private AtomicLong atomicLong = new AtomicLong(0);

    private IdGenertaor idGenertaor = new IdGenertaor();

    /**
     * 饿汉式
     */
    public  IdGenertaor getIdGenertaor(){
        return idGenertaor;
    }

    /**
     * 懒汉式
     * @return
     */
    public synchronized   IdGenertaor getenertaor(){
        if (idGenertaor == null) {
            idGenertaor = new IdGenertaor();
        }
        return idGenertaor;
    }

    /**
     * 双重检测
     * @return
     */
    public  IdGenertaor getIntance(){
        if (idGenertaor == null) {
            synchronized(IdGenertaor.class) {  //类级别加锁
                if (idGenertaor == null) {
                    idGenertaor = new IdGenertaor();
                }
            }
        }
        return idGenertaor;
    }

    public long getId(){
        return atomicLong.getAndIncrement();
    }

}
